package com.atguigu.gmall.list.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.list.model.Goods;
import com.atguigu.gmall.list.model.SearchParam;
import com.atguigu.gmall.list.model.SearchResponseVo;
import com.atguigu.gmall.list.service.SearchService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author: atguigu
 * @create: 2023-09-08 15:59
 */
@RestController
@RequestMapping("api/list")
public class SearchController {

    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Autowired
    private SearchService searchService;

    /**
     * 创建索引库，本着实用主义快速创建索引库
     *
     * @return
     */
    @GetMapping("/createIndex")
    public Result createIndex() {
        //1.创建索引库
        elasticsearchRestTemplate.createIndex(Goods.class);
        //2.设置索引库中字段映射
        elasticsearchRestTemplate.putMapping(Goods.class);
        return Result.ok();
    }


    /**
     * 仅用于测试-将指定sku商品新增到索引库中
     *
     * @param skuId
     * @return
     */
    @ApiOperation("仅用于测试-将指定sku商品新增到索引库中")
    @GetMapping("/inner/upperGoods/{skuId}")
    public Result upperGoods(@PathVariable("skuId") Long skuId) {
        searchService.upperGoods(skuId);
        return Result.ok();
    }

    /**
     * 仅用于测试-将指定sku商品新增到索引库中
     *
     * @param skuId
     * @return
     */
    @ApiOperation("仅用于测试-将指定sku商品进行删除")
    @GetMapping("/inner/lowerGoods/{skuId}")
    public Result lowerGoods(@PathVariable("skuId") Long skuId) {
        searchService.lowerGoods(skuId);
        return Result.ok();
    }


    /**
     * 对指定商品热门分值进行增加/减少
     *
     * @param skuId
     * @param incrscore
     */
    @ApiOperation("对指定商品热门分值进行增加/减少")
    @GetMapping("/inner/incrHotScore/{skuId}/{incrscore}")
    public void incrHotScore(@PathVariable("skuId") String skuId, @PathVariable("incrscore") int incrscore) {
        searchService.incrHotScore(skuId, incrscore);
    }


    /**
     * 站内商品检索接口
     *
     * @param searchParam
     * @return
     */
    @ApiOperation("站内商品检索接口")
    @PostMapping({"", "/"})
    public Result search(@RequestBody SearchParam searchParam) {
        SearchResponseVo vo = searchService.search(searchParam);
        return Result.ok(vo);
    }


}
